//
//  CZSelectFontSizeView.m
//  globalTranslate
//
//  Created by 陈镇 on 2025/8/12.
//

#import "CZSelectFontSizeView.h"
#import "SLUserInfoModel.h"
@interface CZSelectFontSizeView ()

@property(nonatomic,strong) NSArray *sizeList;
@property(nonatomic,strong) NSArray *fontList;
@property(nonatomic,strong) UIButton *selectBtn;

@end
@implementation CZSelectFontSizeView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
     
        [self createButtons];
    }
    return self;
}

-(NSArray *)sizeList{
    if (!_sizeList) {
        _sizeList = @[@"小",@"中",@"大"];
    }
    return _sizeList;
}

+(NSArray *)fontList{

    return @[SLFontMakeRegular(15) ,SLFontMakeRegular(16) ,SLFontMakeRegular(17)];
}


-(void)createButtons{
    
    CGFloat gap = 10;
    CGFloat width = 37;
    for (int i = 0; i<self.sizeList.count; i++) {
        
        QMUIButton *btn = [QMUIButton createButtonTitle:_sizeList[i] titleColor:UIColorHex(202020) font:self.fontList[i]];
        btn.frame = CGRectMake((gap + width) * i, 0, width, width);
        [btn setTitleColor:UIColorWhite forState:UIControlStateSelected];
        [btn sl_cornerRadius:5];
        btn.layer.borderWidth = 0.5;
        btn.tag = i + 100;
        btn.layer.borderColor = UIColorHex(cccccc).CGColor;
        [btn addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
        
        NSString *str = [SLUserInfoModel shareManage].sizeIndex;
        if ([str intValue] == i) {
            [self changeBtn:btn];
        }
        [self addSubview:btn];
        self.size = CGSizeMake(btn.right, btn.height);
        
    }
    
}

-(void)changeBtn:(QMUIButton *)btn{

    _selectBtn.backgroundColor = UIColorWhite;
    _selectBtn.selected = NO;
    _selectBtn = btn;
    _selectBtn.backgroundColor = UIColorHex(#0D6886);
    _selectBtn.selected = YES;
}

-(void)clickItem:(QMUIButton *)btn{
    [self changeBtn:btn];
    NSInteger index = btn.tag - 100;
    [SLUserInfoModel shareManage].sizeIndex = [NSString stringWithFormat:@"%@",@(index)];
    [[SLUserInfoModel shareManage] saveUserInfo];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeSiztFont" object:nil];
    
}
@end
